/*
 * LogsTableModel.java
 *
 * Created on July 13, 2006, 11:00 AM
 *
 */

package org.vcboard.view.model;

import java.awt.event.*;
import javax.swing.Timer;
import javax.swing.table.AbstractTableModel;
import org.vcboard.*;
import org.vcboard.event.*;
import org.vcboard.model.*;
import org.vcboard.view.*;

/**
 * @author Josh
 */
public class LogsTableModel
   extends AbstractTableModel
      implements LogsUpdatedListener
{
   String[] cols = {
      "Date",
      "Clock In",
      "Clock Out",
      "Duration"
   };
   
   boolean isLoggedIn = false;
   
   /** Creates a new instance of LogsTableModel */
   public LogsTableModel()
   {
      WorkTimer.addListener(this);
   }
   
   public Object getValueAt(int rowIndex, int columnIndex)
   {
      TimeLog tmp = WorkTimer.getLogs().get(rowIndex);
      Object retVal;
      
      if (tmp.getLogId() != -1)
      {
         switch (columnIndex)
         {
            case 0:
               retVal = tmp.getDate();
               break;
            case 1:
               retVal = tmp.getClockIn().getTime();
               break;
            case 2:
               if (tmp.getClockOut() == null)
               {
                  retVal = "Still clocked in...";
                  isLoggedIn = true;
               }
               else
               {
                  retVal = tmp.getClockOut().getTime();
               }
               break;
            case 3:
               retVal = tmp.getDuration();
               break;
            default:
               retVal = tmp;
         }
      }
      else
      {
         switch (columnIndex)
         {
            case 0:
               retVal = tmp.getDate();
               break;
            case 3:
               retVal = tmp.getDuration();
               break;
            default:
               retVal = "";
         }
      }
      
      return retVal;
   }

   public int getRowCount()
   {
      return WorkTimer.getLogs().size();
   }

   public int getColumnCount()
   {
      return cols.length;
   }

   public String getColumnName(int column)
   {
      return cols[column];
   }

   public void updateLogs()
   {
      isLoggedIn = false;
      fireTableDataChanged();
      WorkTimerGUI.getInstance().timerUpdate();
      WorkTimerGUI.getInstance().setTooltips();
   }

   public boolean isCellEditable(int rowIndex, int columnIndex)
   {
      return false;
   }
   
}
